<?php
namespace app\beautiful\logic\order;

use app\beautiful\model\member\MemberIntegralModel;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\order\GoodsOrderModel;

trait OrderTrait {
    
    protected $member;
   
    protected $shopId;
    protected $today;
    protected $orderId;
    protected $order;
    public function __construct($shopId) {
        $this->shopId = $shopId;
        $this->today = date('Y-m-d',time());
    }

    public function checkMember($memberId,$lock = true){
        empty($memberId) && $this->error ('用户未登录');
        if(null === $this->member = MemberModel::where('member_id',$memberId)->lock($lock)->find()){
            $this->error('用户未登录');
        }
        ($this->member->shop_id != $this->shopId) && $this->error('用户不是该APP的');
        $integral = MemberIntegralModel::where([
                ['shop_id','=',$this->shopId],
                ['member_id','=',$memberId],
                ['expire_time','>',time()]
            ])->sum('remain_integral') ?? 0;
        $this->member->integral=$integral;
    }

    public function checkOrder($orderId,$lock=true){
        empty($orderId) &&  $this->error('请选择要支付的订单');
        if(null === $this->order = GoodsOrderModel::where('order_id',$orderId)->lock($lock)->find()){
            $this->error('订单不存在');
        }
        if($this->member->shop_id != $this->shopId){
            $this->error('订单不存在');
        }
    }
   
    
    public function error($message){
        throw new \Exception($message);
    }


    
    
}
